package com.shop.shop_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.shop_end.entity.Complain;
import com.shop.shop_end.exception.ServiceException;
import com.shop.shop_end.mapper.ComplainMapper;
import com.shop.shop_end.service.ComplainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.shop_end.utils.DateUtils;
import com.shop.shop_end.utils.result.Result;
import com.shop.shop_end.utils.result.ResultUtil;
import com.shop.shop_end.variable.EnableVariable;
import com.shop.shop_end.variable.ErrorCodeEnum;
import com.shop.shop_end.vo.ComplainVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.x509.EDIPartyName;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2024-04-23
 */
@Service
public class ComplainServiceImpl extends ServiceImpl<ComplainMapper, Complain> implements ComplainService {
    final Logger logger = LoggerFactory.getLogger(ComplainServiceImpl.class);

    @Resource
    private ComplainMapper complainMapper;

    @Override // 继承接口的新增 返回值0(失败),1(成功)
    @Transactional
    public Result insertComplain(Complain complain) {
        complain.setCreateTime(new Date());
        complain.setStatus("未回复");
        try {
            int insert = this.complainMapper.insert(complain);
            if (insert == EnableVariable.UNABLE){
                logger.info("增加失败");
                throw new ServiceException(ErrorCodeEnum.SQL_INSERT_ERROR, "增加失败");
            }
            logger.info("增加成功");
            return ResultUtil.ok("增加成功", insert);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("增加出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_INSERT_ERROR, "增加出现异常");
        }
    }

    @Override // 继承接口的更新 返回值0(失败),1(成功)
    @Transactional
    public Result updateComplain(Complain complain) {
        try {
            int update = this.complainMapper.updateById(complain);
            if (update == EnableVariable.UNABLE){
                logger.info("修改失败");
                throw new ServiceException(ErrorCodeEnum.SQL_UPDATE_ERROR, "修改失败");
            }
            logger.info("修改成功");
            return ResultUtil.ok("修改成功", update);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("修改出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_UPDATE_ERROR, "修改出现异常");
        }
    }

    @Override // 继承接口的删除 返回值0(失败),1(成功)
    @Transactional
    public Result deleteComplain(Long complainId) {
        try {
            int delete = this.complainMapper.deleteById(complainId);
            if (delete == EnableVariable.UNABLE){
                logger.info("删除失败");
                throw new ServiceException(ErrorCodeEnum.SQL_DELETE_ERROR, "删除失败");
            }
            logger.info("删除成功");
            return ResultUtil.ok("删除成功", delete);
        }catch (Exception e){
            e.printStackTrace();
            logger.info("删除出现异常");
            throw new ServiceException(ErrorCodeEnum.SQL_DELETE_ERROR, "删除出现异常");
        }
    }

    @Override // 继承接口的查询全部
    public List<ComplainVo> getAllComplain() {
        List<Complain> complainList = this.complainMapper.selectList(new QueryWrapper<Complain>()
                .orderByDesc("createTime"));
        List<ComplainVo> complainVoList = new ArrayList<>();
        complainList.forEach((item)->{
            ComplainVo complainVo = new ComplainVo();
            complainVo.setComplainId(item.getComplainId());
            complainVo.setContents(item.getContents());
            complainVo.setCreateTime(DateUtils.format(item.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
            complainVo.setReps(item.getReps());
            complainVo.setStatus(item.getStatus());
            complainVo.setUserId(item.getUserId());
            complainVo.setUsername(item.getUsername());
            complainVo.setTitle(item.getTitle());
            complainVoList.add(complainVo);
        });
        logger.info("complainVoList==>{}", complainVoList);
        return complainVoList;
    }

    @Override // 继承接口的按主键查询 返回Entity实例
    public ComplainVo getComplainById(Long complainId) {
        Complain complain = this.complainMapper.selectById(complainId);
        ComplainVo complainVo = new ComplainVo();
        if (complain != null){
            complainVo.setComplainId(complain.getComplainId());
            complainVo.setContents(complain.getContents());
            complainVo.setCreateTime(DateUtils.format(complain.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
            complainVo.setReps(complain.getReps());
            complainVo.setStatus(complain.getStatus());
            complainVo.setUserId(complain.getUserId());
            complainVo.setUsername(complain.getUsername());
            complainVo.setTitle(complain.getTitle());
        }
        return complainVo;
    }

}
